<?php
/**
 * The index controller of the blog module.
 *
 * @author Flavius Aspra <flavius.as@gmail.com>
 * @link http://yii-modules.yet-another-project.com
 * @copyright 2010 &copy; Flavius Aspra
 * @licence http://yii-modules.yet-another-project.com/license/
 * @package blog
 * @version $Id: IndexController.php 55 2010-05-01 21:02:07Z flavius.as $
 * @todo it contains code which may need a cleanup
 */
class IndexController extends Controller {
	private $feeds = array(
		'index' => '/blog/index/feed',
	);
	protected function beforeAction($a) {
		$this->insertFeed($a);
		$this->createSidebars($a);
		return TRUE;
	}
	public function actionIndex() {
		$user = NULL;
		if($this->module->hasFeature('User')) {
			$user = User::model()->findByPk(Yii::app()->user->id);
		}
		if($this->hasContextRights()) {
			$posts = BlogPost::model()->findAll();
		}
		else {
			$posts = BlogPost::model()->published()->findAll();
		}
		$this->renderParents('index',array('posts' => $posts,'user' => $user));
	}
	public function actionShowpost() {
		if($this->hasContextRights()) {
			$post = BlogPost::model()->findByPk($_GET['id']);
			$this->renderParents('showpost',array('post' => $post));
		}
		else {
			Yii::app()->user->loginRequired();
		}
	}
	public function actionNewpost() {
		if($this->hasContextRights()) {
			if(isset($_GET['id'])) {
				$model = BlogPost::model()->findByPk($_GET['id']);
				$context = 'Edit';
			}
			if(!isset($model) || !$model) {
				$model = new BlogPost;
				$context = 'Create';
			}
			$form = new CForm('blog.views.index.postForm',$model);
			$form->title = $context . $form->title;
			if($form->submitted('save') && $form->validate()) {
				$model->attributes = $_POST['BlogPost'];
				if($model->save()) {
					$this->redirect(array('/blog/index/showpost','id' => $model->id));
				}
			}
			$this->renderParents('newpost',array('form' => $form));
		}
		else {
			Yii::app()->user->loginRequired();
		}
	}
	public function actionFeed() {
		Yii::import('application.vendors.*');
		require_once 'Zend/Feed.php';
		require_once 'Zend/Feed/Atom.php';
		$posts = BlogPost::model()->findAll();
		$entries = array();
		foreach($posts as $post) {
			$entries[] = array(
				'title' => $post->title,
				'link' => CHtml::encode($this->createAbsoluteUrl('/blog/index/showpost',array('id' => $post->id))),
				//'link' => $this->createAbsoluteUrl('/blog/index/showpost',array('id' => $post->id)),
				'description' => $post->getIntro(TRUE),
				'lastUpdate' => $post->create_time,
			);
		}
		$feed = Zend_Feed::importArray(
			array(
				//@todo configuration directives here
				'title' => 'Flavius\' Blog',
				'link' => CHtml::encode($this->createAbsoluteUrl('/blog/index/index')),
				'charset' => 'UTF-8',
				'entries' => $entries,
			),
			'atom'
		);
		$feed->send();
	}
	private function insertFeed(CAction $a) {
		if(array_key_exists($a->id,$this->feeds)) {
			Yii::app()->clientScript->registerLinkTag('alternate','application/atom+xml',$this->createUrl($this->feeds[$a->id]));
		}
	}
	protected function createSidebars($a) {
		//@todo raise pre-event
		if(in_array($a->id,array('index','showpost'))) {
			if($this->hasContextRights()) {
				$content = CHtml::link('new post',array('/blog/index/newpost'));
				if('showpost' === $a->id) {
					$content .= '<br />'.CHtml::link('edit post',array('/blog/index/newpost','id' => $_GET['id']));
				}
			}
		}
		//@todo raise post-event
	}
	/**
	 * hasContextRights whether plugged in modules and configurations allow full access
	 * (admin sidebar, editing posts, viewing drafts)
	 * 
	 * @access protected
	 * @return void
	 */
	protected function hasContextRights() {
		if(!$this->module->useUsers ||
			$this->module->useUsers && !Yii::app()->user->isGuest) {//@todo or RBAC
				return TRUE;
		}
		return FALSE;
	}
}
